package cn.billy.basic.service.impl;

import cn.billy.basic.mapper.BaseMapper;
import cn.billy.basic.query.BaseQuery;
import cn.billy.basic.service.IBaseService;
import cn.billy.basic.utils.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {

    @Autowired
    private BaseMapper<T> baseMapper;
    /*
     * @Title: loadAll
     * @Description: 查询全部
     * @Author: Billy
     * @Version: 1.0
     * @Date:  2022/6/21 21:22
     * @Parameters:
     * @Return
     */
    @Override
    public List<T> loadAll() {
        return baseMapper.loadAll();
    }

    /*
     * @Title: getById
     * @Description: 根据id查询
     * @Author: Billy
     * @Version: 1.0
     * @Date:  2022/6/21 21:23
     * @Parameters:
     * @Return
     */
    @Override
    public T getById(Long id) {
        return baseMapper.getById(id);
    }

    /*
     * @Title: remove
     * @Description: 删除
     * @Author: Billy
     * @Version: 1.0
     * @Date:  2022/6/21 21:23
     * @Parameters:
     * @Return
     */
    @Override
    @Transactional
    public void remove(Long id) {
        baseMapper.remove(id);
    }

    /*
     * @Title: save
     * @Description: 新增
     * @Author: Billy
     * @Version: 1.0
     * @Date:  2022/6/21 21:23
     * @Parameters:
     * @Return
     */
    @Override
    @Transactional
    public void save(T t) {

        baseMapper.add(t);
    }

    /*
     * @Title: update
     * @Description: 修改
     * @Author: Billy
     * @Version: 1.0
     * @Date:  2022/6/21 21:23
     * @Parameters:
     * @Return
     */
    @Override
    @Transactional
    public void update(T t) {
        baseMapper.update(t);
    }

    /*
     * @Title: queryData
     * @Description: 分页+高级查询
     * @Author: Billy
     * @Version: 1.0
     * @Date:  2022/6/21 21:24
     * @Parameters:
     * @Return
     */
    @Override
    public PageList<T> queryData(BaseQuery baseQuery) {
        Integer total = baseMapper.queryCount(baseQuery);
        List<T> rows = baseMapper.queryData(baseQuery);
        return new PageList<>(total,rows);
    }

    @Override
    public void batchDel(Long[] ids) {
        baseMapper.batchDel(ids);
    }
}
